#!/usr/bin/env bash
<<_
#############################################################
+-----------------------------------------------------------+
|                     ! BUILD SETTINGS !                    |
+-----------------------------------------------------------+
#############################################################
_

### BEGIN CONFIG INFO
# Povides:	configure.x
# Step - 1	checking packages
# step - 2	make depends
# step - 3	add services
# step - 4	build daemon
# step - 5	finalize
### END CONFIG INFO

[ $UID == 0 ] || $(echo "You must be root to build $"; exit 0) 2> /dev/null

#//////////////////////////////////////////////
#// checking de la configuration systeme
#//////////////////////////////////////////

readlink -f /proc/$$/exe | grep -q bash || {
    echo >&2 "You must run $0 with bash!"
    exit 2
}


if [[ 0 -eq `echo $SHELL|grep --perl-regexp sh &>/dev/null; echo $?` ]]
    then GRP='--perl-regexp'
else
    GRP='--extended-regexp'
fi
if [[ 1 -eq `grep --help|grep --count color` ]]
    then GRP='--color=auto '$GRP ;fi
alias grep='grep --ignore-case '$GRP
unset GRP


#//////////////////////////////////////////////
#// configuration de l'environnement
#//////////////////////////////////////////

OS=$(lsb_release -si | tr '[A-Z]' '[a-z]')
ARCH=$(uname -m)
PATH=/usr/bin:/bin
USRBIN=/usr/bin
USRLOCALBIN=/usr/local/bin
USRLIB=/usr/lib
LOCALLIB=./lib
MANUAL=./man
umask 022

#//////////////////////////////////////////////
#// invocation des fonctions anexes
#//////////////////////////////////////////

test -f ./utils/primary-functions && . ./utils/primary-functions
test -f ./utils/secondary-functions && . ./utils/secondary-functions

#//////////////////////////////////////////////
#// checking des dependences + librairies
#//////////////////////////////////////////

tput clear
col=`tput cols`
nb=`ls -l|wc -l|nawk '{print $1}'`
lst=`ls -l|awk '{print $8}'`
u=`tput cup 2 0`
o=`tput cup 1 0`
v=`tput cup 5 0`
c=`tput el`
i=1
p=""

GREEN="\033[32;42m"
CYAN="\033[36;1m"
OFF="\033[0m"

declare -a steps=('checking-packages' 'make-depends' 'add-services' 'building-daemon' 'Finalize')
txt=`echo -e "Configure progression\n"`
for t in $(echo ${steps[@]})
do
	pc=`echo "scale=3; ($i/$nb)+1"|bc`
	pct=`echo "(($pc-1)*100)"|bc|nawk 'BEGIN {FS="."} {print $1}'`
	pct2=`echo "(($pc-1)*($col-20))+1"|bc|nawk 'BEGIN {FS="."} {print $1}'`
	j=0
	p=""

	while [ $j -lt $pct2 ]
	do
		p=${p}"="
		j=$(($j+1))
	done
clear
	echo -e "${o}${CYAN}[ $pct% ]${OFF}${GREEN}${p}>${OFF}${c}[ ${t} ]${u}${c}${txt}"
	step_$i

sleep 2

	i=$(($i+1))
done;
echo -e "${o}${CYAN}[ 100% ]${OFF}${GREEN}${p}>${OFF}${c}[ ${t} ]${u}${c}Finish"
